package com.sms.web.domain.service.impl;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sms.common.persistence.DynamicSpecifications;
import com.sms.common.persistence.SearchFilter;
import com.sms.domain.entity.Template;
import com.sms.domain.service.TemplateService;
import com.sms.web.domain.dao.TemplateDao;

/**
 * @desc 模板管理
 * @author wjw
 * @time 2017年11月21日上午10:44:19
 */
@Service
@Transactional
public class TemplateServiceImpl implements TemplateService{
    
    protected Logger logger = LoggerFactory.getLogger(getClass());
    
    @Autowired
    private TemplateDao templateDao;
    
    @Override
    @Transactional(readOnly = true)
    public Template getOne(String id) {
        return templateDao.getOne(id);
    }
    
    @Override
    @Transactional(readOnly = true)
    public Template findByAppAndCode(String appId, String templateCode) {
    	return templateDao.findByAppAndCode(appId, templateCode);
    }
    
    @Override
    @Transactional(readOnly = true)
    public Page<Template> findAll(Map<String, Object> searchParams, int pageNumber, int pageSize) {
        PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, new Sort(Direction.DESC, "templateId"));
        Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
        Specification<Template> spec = DynamicSpecifications.bySearchFilter(filters.values(), Template.class);
        return templateDao.findAll(spec, pageRequest);
    }
    
    
    @Override
    @Transactional(readOnly = true)
    public Page<Template> findAll(int pageSize, int page) {
        Pageable pageable = new PageRequest(page, pageSize, new Sort(Direction.DESC, "templateId"));
        return templateDao.findAll(pageable);
    }
    
    /**
     * 保存实体
     * @param template xx
     * @return xx
     */
    @Override
    public Template save(Template template) {
        return templateDao.save(template);
    }
    
    @Override
    public void delete(String templateId) {
        templateDao.deleteById(templateId);
    }
    
}
